﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Media.Imaging;

namespace Utilities
{
    public class Image
    {
        /// <summary>
        /// Returns the size, in pixels, of the image pointed to by f. Mostly (I hope) without
        /// having to open it.
        /// </summary>
        /// <param name="f"></param>
        /// <returns></returns>
        public static int[] GetImageSize(FileInfo f)
        {
            using (FileStream imageStream = f.OpenRead())
            {
                BitmapDecoder dc = BitmapDecoder.Create(imageStream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
                if (dc.Frames.Count == 0)
                {
                    return null;
                }
                double xsize = dc.Frames[0].Width / 96.0 * dc.Frames[0].DpiX;
                double ysize = dc.Frames[0].Height / 96.0 * dc.Frames[0].DpiY;

                imageStream.Close();

                return new int[] { (int)xsize, (int)ysize };
            }
        }

        /// <summary>
        /// Rotate to a new madeup name.
        /// </summary>
        /// <param name="picCopy"></param>
        public static FileInfo Rotate(FileInfo pic)
        {
            string name = pic.Name.Substring(0, pic.Name.Length - pic.Extension.Length);
            FileInfo result = new FileInfo(pic.Directory.FullName + "\\" + name + "_R.jpg");

            Rotate(pic, result);

            return result;
        }

        /// <summary>
        /// Will rotate the picture from the source to the dest picture.
        /// </summary>
        /// <param name="sourcePic"></param>
        /// <param name="destPic"></param>
        private static void Rotate(FileInfo sourcePic, FileInfo destPic)
        {
            Rotate(sourcePic, destPic, true);
        }

        private static void Rotate (FileInfo sourcePic, FileInfo destPic, bool rotateClockwise)
        {
            BitmapImage bmi = new BitmapImage();
            using (FileStream sourceStream = sourcePic.OpenRead())
            {
                bmi.BeginInit();
                //bmi.UriSource = new Uri(sourcePic.FullName);
                bmi.StreamSource = sourceStream;
                if (rotateClockwise)
                {
                    bmi.Rotation = Rotation.Rotate90;
                }
                else
                {
                    bmi.Rotation = Rotation.Rotate270;
                }
                bmi.EndInit();

                FormatConvertedBitmap newBitMap = new FormatConvertedBitmap();
                newBitMap.BeginInit();
                newBitMap.Source = bmi;
                newBitMap.EndInit();

                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                encoder.QualityLevel = 100;
                encoder.Frames.Add(BitmapFrame.Create(newBitMap));
                using (FileStream output = destPic.Create())
                {
                    encoder.Save(output);
                    output.Close();
                }
                sourceStream.Close();
            }
        }

        /// <summary>
        /// Rotate an image in place. Don't screw up, we will overwrite everything!
        /// </summary>
        /// <param name="pic"></param>
        public static void RotateInPlace(FileInfo pic)
        {
            RotateInPlace(pic, true);
        }

        /// <summary>
        /// Rotate in place, select which 90 degree way.
        /// </summary>
        /// <param name="image"></param>
        /// <param name="rotate90"></param>
        public static void RotateInPlace(FileInfo image, bool rotate90)
        {
            FileInfo destFile = new FileInfo(Path.GetTempFileName());
            Rotate(image, destFile, rotate90);
            destFile.CopyTo(image.FullName, true);
            destFile.Delete();
        }

        /// <summary>
        /// Test to see if this is in landscape or portrait mode.
        /// </summary>
        /// <param name="picCopy"></param>
        /// <returns></returns>
        public static bool IsLandscape(FileInfo picCopy)
        {
            int[] sizes = GetImageSize(picCopy);

            return sizes[0] >= sizes[1];
        }

    }
}
